<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Ad;
use app\common\model\ArticleLike;
use app\common\model\Content;
use app\common\model\Iplog;
use app\common\model\Like;
use app\common\model\Unlike;
use app\common\model\UserLoginLog;
use app\service\ContentService;
use app\service\EmailService;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;
use think\Request;

class Index extends Common
{

    public function __construct()
    {
        parent::__construct();
        $ad = Ad::where('board_id',7)->select();
        View::assign("ad",$ad);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {


        $cid = input("get.cid");
        if($cid){
            $where["cate_id"] = $cid;
        }
        $where["status"] =1;
        $where["model_id"] =1;


        if(!isset($where)){
            $where = 1;
        }

        $list = \app\common\model\Content::with("user")
            ->where($where)
            ->order("top DESC,recommend DESC,id DESC")
            ->paginate([
                'query'=> $this->params,
            ]);

        $temp = $list->items();
        foreach ($temp as $k => $v){
            //更新文章的展现量
            update_hits($v["id"]);
        }

        //echo Db::name("article")->getLastSql();die;
        View::assign("list",$list);
        return  view();
    }

    public function search()
    {
        $keywords = \think\facade\Request::get("keywords");
        //将用户搜索的关键词存入到tag表中
        $res = Db::name("tags")
            ->whereLike('name',"%{$keywords}%")
            ->find();
        if(!$res){
            $temp["name"] = $keywords;
            $temp["status"] = 0;
            $temp["ip"] = \request()->ip();
            $temp["hits"] = 0;
            $temp["create_time"] = time();
            if(session("index_user")){
                $user = session("index_user");
                $temp["uid"] = $user["id"];
            }else{
                $temp["uid"] = 0;
            }
            Db::name("tags")->insert($temp);
        }else{
            Db::name("tags")
                ->whereLike('name',"%{$keywords}%")
                ->inc("hits",1)
                ->update();
        }
        if($keywords){
            $keywords = trim($keywords);
            View::assign('keywords',$keywords);
        }
        $list = \app\common\model\Content::with("user")
            ->whereLike('title',"%{$keywords}%")
            ->order("id DESC")
            ->paginate([
                'query'=> $this->params,
            ]);
        $temp = $list->items();
        foreach ($temp as $k => $v){
            //更新文章的展现量
            update_hits($v["id"]);
        }

        //echo Db::name("article")->getLastSql();die;
        View::assign("list",$list);
        return  view('index');
    }



    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function cache_clear()
    {
        Cache::clear();
    }

    //简历模板
    public function jianli()
    {
        View::assign("list",'');
        return  view();
    }

    public function push_to_baidu()
    {
        $url[] = \request()->param("url");
        $res = push_to_baidu($url);
        return (string)($res);
    }

    public function test()
    {

    }

    //每日报道
    public function ropot_every_day()
    {
        $today = strtotime(date('Y-m-d'));
        $count_article = Content::where("add_time",">",$today)
            ->count("id");
        $count_comment = \app\common\model\Comment::where("create_time",">",$today)
            ->count("id");
        $count_active_users = UserLoginLog::where("create_time",">",$today)
            ->count("id");
        $count_ip_logs = Iplog::where("create_time",">",$today)
            ->count("id");
        $count_ip_logs_by_ip = Iplog::where("create_time",">",$today)
            ->group("ip")
            ->count("id");
        $msg = "米醋儿网每日分析报告：
            今日发稿{$count_article}篇，
            新增留言{$count_comment}条，
            活跃用户{$count_active_users}个，
            pv数{$count_ip_logs}，
            ip{$count_ip_logs_by_ip}个,
            以上是简单报道。";
        $res = EmailService::ropot('571031767@qq.com',$msg);
        return $msg;
    }
    public function RandAbc($length = "") { // 返回随机字符串
        $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        return str_shuffle($str);
    }

    /*
     * $content 文章内容
     * $order 要获取哪张图片，ALL所有图片，0第一张图片
    */
    public  function getImgs($content,$order='ALL')
    {
        $pattern = "/<img .*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
        preg_match_all($pattern,$content,$match);
        if(isset($match[1]) && !empty($match[1])){
            if($order==='ALL'){
                return $match[1];
            }
            if(is_numeric($order) && isset($match[1][$order])){
                return $match[1][$order];
            }
        }
        return '';
    }


    //今日热门
    public  function hot_today()
    {
        $hot = ContentService::hot_day();
        View::assign("list",$hot);
        return  view();
    }
    public  function hot_in_seven_day()
    {
        $params["type"] = "-7 days";
        $params["limit"] = 20;
        $hot = ContentService::hot_day($params);
        View::assign("list",$hot);
        return  view('hot_today');
    }
    public  function hot_in_thirty_days()
    {
        $params["type"] = "-30 days";
        $params["limit"] = 50;
        $hot = ContentService::hot_day($params);
        View::assign("list",$hot);
        return  view('hot_today');
    }



}
